package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Set;

@RestController
@RequestMapping("/role")
public class RoleController {

    @Reference
    private RoleService roleService;

    /**
     * 用于显示新增用户表中角色信息
     * @return
     */
    @RequestMapping("/findAll")
    public Result findAll(){
        try {
            Set<Role> set = roleService.findAll();
            return new Result(true, "查询角色成功",set);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, "查询角色失败");
        }
    }

    /**
     * 根据条件分页查询角色
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/findByCondition")
    public PageResult findByCondition(@RequestBody QueryPageBean queryPageBean){

        try{
           return roleService.findByCondition(queryPageBean);

        }catch(Exception e){
            e.printStackTrace();
            return null;
        }
    }


    //添加检查组
    @RequestMapping("/add")
    public Result addCheckGroup(@RequestBody Role role, Integer[] permissionIds){

        try {
            //调用service层方法
            roleService.addRole(role,permissionIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_ROLE_FAIL);
        }
        return new Result(true,MessageConstant.ADD_ROLE_SUCCESS);
    }

    //根据id获取角色基本信息
    @RequestMapping("/findById/{id}")
    public Result findRole(@PathVariable Integer id){

        Role role=null;
        try {
            //调用service层方法
            role = roleService.findRole(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
        return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,role);
    }

    /**
     * 修改角色信息
     * @param role
     * @param permissionIds
     * @return
     */
    @RequestMapping("/update")
    public Result updateRole(@RequestBody Role role,Integer[] permissionIds){
        try {
            //调用service层方法
            roleService.updateRole(role,permissionIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_ROLE_FAIL);
        }
        return new Result(true,MessageConstant.EDIT_ROLE_SUCCESS);
    }

    /**
     * 删除角色
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    public Result delete(Integer id){
        try{
            roleService.delete(id);
            return new Result(true, MessageConstant.DELETE_ROLE_SUCCESS);
        }catch(Exception e){
            return new Result(false, MessageConstant.DELETE_ROLE_FAIL);
        }
    }
}
